/**
 * 
 */
package com.wisdytech.linkdcs.system.action;


import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.bean.StandardResult;
import com.wisdytech.common.bean.StandardTableResult;
import com.wisdytech.common.handler.ResourceMatchingService;
import com.wisdytech.common.utils.DataTransformUtil;
import com.wisdytech.linkdcs.system.model.SysRole;
import com.wisdytech.linkdcs.system.service.ISystemResourceService;
import com.wisdytech.linkdcs.system.service.ISystemRoleService;
import com.wisdytech.linkdcs.system.vo.ResourceNodeVO;
import com.wisdytech.linkdcs.system.vo.RoleVO;
import com.wisdytech.linkdcs.system.vo.TreeviewNodeVO;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * ClassName: SystemRoleAction
 * </p>
 * <p>
 * Description: 角色管理
 * </p>
 * <p>
 * Author: kongming
 * </p>
 * <p>
 * Date: 2017-9-13
 * </p>
 */
@Controller
@RequestMapping("/system/role")
public class SystemRoleAction {
    
    /**
     * 角色sysRoleService
     */
    @Autowired
    @Qualifier("systemRoleService")
    private ISystemRoleService sysRoleService;
    @Autowired
    @Qualifier("resourceMatchingService")
    private ResourceMatchingService resourceMatchingService;
    /**
     * 资源sysResourceService
     */
    @Autowired
    @Qualifier("systemResourceService")
    private ISystemResourceService sysResourceService;
    
    
    @RequestMapping("/index")
    public String index(Model model, HttpServletRequest httpRequest){
        this.resourceMatchingService.getResourceName(httpRequest,model);
        return "system/role/roleIndex";
    }
    
    /**
     * <p>Description: 分页条件查询角色信息</p> 
     * @param roleName 角色名称
     * @param roleType 角色类别
     * @param filter 分页参数
     * @return 角色信息列表
     * @throws IOException 
     */
    @RequestMapping(value = "/listSysRoles/get.mvc")
    @ResponseBody
    public StandardTableResult<RoleVO> listSysRoles(String roleName, String roleType, Filter filter) throws IOException{
        PageInfo<SysRole> sysRolePage = this.sysRoleService.listSysRoles(roleName, roleType, filter);
        //转换为前台结果集
        List<SysRole> roleList = sysRolePage.getList();
        List<RoleVO> roleVOList = new ArrayList<RoleVO>();
        for(SysRole sysRole:roleList){
            roleVOList.add(DataTransformUtil.changeUser2UserVO(sysRole));
        }
        return new StandardTableResult<RoleVO>(sysRolePage.getTotal(),roleVOList);
    }
    
    @RequestMapping(value="/getSysRole/get.mvc")
    @ResponseBody
    public StandardResult getSysRoleById(String id){
        if(StringUtils.isBlank(id)){
            return StandardResult.getFailResult("角色不存在");
        }
        SysRole role =sysRoleService.getSysRoleById(id);
        return StandardResult.getSuccessResult(role);
    }
    
    /**
     * <p>Description: 删除角色信息</p> 
     * @param id 角色唯一标识id
     * @return boolean值
     */
    @RequestMapping(value = "/delSystemRoleById/del.mvc")
    @ResponseBody
    public StandardResult delSysRoleById(String id) {
        return sysRoleService.delSystemRoleById(id);
    }
    
    /**
     * <p>Description: 保存角色</p> 
     * @param id 角色唯一标识id
     * @return boolean值
     */
    @RequestMapping(value = "/saveRole/save.mvc")
    @ResponseBody
    public StandardResult saveUser(SysRole sysRole) {
        if (StringUtils.isBlank(sysRole.getPkId())) {
            return sysRoleService.addSystemRole(sysRole);
        }else {
            return sysRoleService.updateSystemRole(sysRole);
        }
    }

    /**
     * <p>Description:查询角色信息列表</p> 
     * @return 角色信息列表集合
     */
    @RequestMapping(value = "/listAllRole/get.mvc")
    @ResponseBody
    public List<SysRole> listAllRole(){
        return sysRoleService.listAllRole();
    }
    
    /**
     * 
     * <p>Description: 根据角色id获取用户资源</p>
     * @param roleId
     * @return
     */
    @RequestMapping(value="/listRoleResource/get.mvc")
    @ResponseBody
    public StandardResult listRoleResourcs(String roleId){
        if(StringUtils.isBlank(roleId)){
            return StandardResult.getFailResult();
        }
        List<TreeviewNodeVO>  resources=
                sysResourceService.getResourceTree(roleId);
        return StandardResult.getSuccessResult(resources);
    }
    
    

}
